<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleDialogClose"
    :lock-scroll="false"
    :close-on-click-modal="showMobilePage"
    :modal-append-to-body="false"
    :class="{'global-mobile-dialog': showMobilePage}"
    class="subcribe-common-dialog small-common-dialog-size"
  >
    <DialogTitle title="合并角色" font-size="16px" style="margin-bottom:20px;">
      <el-tooltip placement="top">
        <div slot="content" style="padding: 4px 0">
          <div>1、选择的角色将作为主体合并;</div>
          <div>2、合并后，所有角色的权限去重后取并集;</div>
          <div>3、合并后，所有角色中的操作员去重后取并集。</div>
        </div>
        <tipIcon is-diy style="margin-left: 4px" />
      </el-tooltip>
    </DialogTitle>
    <el-form
      ref="roleMergeForm"
      :model="roleData"
      :rules="rules"
      :label-position="showMobilePage?'top':'right'"
      label-width="80px"
      class="lineHeight32 mb0">
      <el-form-item label="主体角色" prop="mainRoleId">
        <el-select v-model="roleData.mainRoleId" popper-class="global-select-down" placeholder="请选择主体角色">
          <el-option
            v-for="item in roleList"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleId" />
        </el-select>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button class="ok" type="primary" @click="handleConfirm">确定</el-button>
      <el-button class="cancel-btn-style" type="info" @click="handleDialogClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import DialogTitle from '@/components/Title';
import { mapState } from 'vuex';

export default {
  name: 'DeptDialog',
  components: {
    DialogTitle
  },
  props: {
    roleList: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleData: {
        mainRoleId: null
      },
      dialogVisible: false,
      modalMaskDom: null
    };
  },
  computed: {
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    }),
    rules() {
      return {
        'mainRoleId': [{ required: true, message: '请选择主体角色', trigger: 'change' }]
      };
    }
  },
  watch: {
    loading(val) {
      const modalMaskDom = document.querySelector('.v-modal');
      modalMaskDom && (modalMaskDom.style.zIndex = val ? -1 : 2000);
    }
  },
  methods: {
    handleDialogClose() {
      this.$refs['roleMergeForm'].resetFields();
      this.dialogVisible = false;
    },
    handleConfirm() {
      this.$refs['roleMergeForm'].validate(valid => {
        if (valid) {
          this.$emit('handleRolesMerge', this.roleData.mainRoleId);
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: center;
  margin-top: 25px;
  .ok {
    width: 120px;
    font-size: 16px;
    padding: 8px 20px;
  }
  .cancel-btn-style {
    font-size: 14px;
    padding: 8px 20px;
    margin-left: 16px !important;
  }
}
::v-deep .el-form-item__content {
  width: 240px;
  margin-left: 0 !important;
  .el-input__suffix {
    display: flex;
    align-items: center;
  }
  .el-input__inner {
    height: 32px;
    line-height: 32px;
    width: 240px;
    font-size: 14px;
  }
}
::v-deep .el-form-item {
  display: flex;
}
.global-mobile-dialog {
  ::v-deep .el-form-item__content {
    margin-left: 6px !important;
  }
}
</style>
